#include "button_home_put.h"

button_home_put::button_home_put(QWidget *parent) : Button_Cust_Base(parent)
  , isOn(true)
{
    //this->setFixedSize(50,30);
    this->setScaledContents(true);
    this->setPixmap(onLeaveImage);

}

void button_home_put::enterEvent(QEvent *event)
{
    Q_UNUSED(event);
    if (isOn) {
        this->setPixmap(offPressImage);
    } else {
        this->setPixmap(onPressImage);
    }
}

void button_home_put::leaveEvent(QEvent *event)
{
    Q_UNUSED(event);
    updateImage();
}

void button_home_put::mousePressEvent(QMouseEvent *event)
{
    Qt::MouseButton button = event->button();
    if (button == Qt::LeftButton) {
        if (isOn) {
            this->setPixmap(offLeaveImage);
        } else {
            this->setPixmap(onLeaveImage);
        }
    }
    // 调用基类的鼠标按下事件处理（如果需要）
    // Button_Cust_Base::mousePressEvent(event);
}

void button_home_put::mouseReleaseEvent(QMouseEvent *event)
{
    Qt::MouseButton button = event->button();
    QPoint mousePos = event->globalPos();
    QRect widgetRect = this->rect();
    if(widgetRect.contains(this->mapFromGlobal(mousePos))){
        if (button == Qt::LeftButton) {
            isOn = !isOn; // 切换状态
            emit stateChanged(isOn);
            if (isOn) {
                this->setPixmap(onLeaveImage);
            } else {
                this->setPixmap(offLeaveImage);
            }
        }
    }
    // 调用基类的鼠标释放事件处理（如果需要）
    // Button_Cust_Base::mouseReleaseEvent(event);
}

void button_home_put::updateImage()
{
    if (isOn) {
        this->setPixmap(onLeaveImage);
    } else {
        this->setPixmap(offLeaveImage);
    }
}
